//
//  UICollectionViewEx.swift
//  YPWatermarkCamera
//
//  Created by Json on 2023/8/2.
//

import Foundation

extension UICollectionView {
    
    func wmc_register(cellClass: UICollectionViewCell.Type) {
        let identifier = Self.identifier(for: cellClass)
        register(cellClass, forCellWithReuseIdentifier: identifier)
    }
    
    /// 批量注册UICollectionViewCell
    /// - Parameter cellClasses: cell类型数组
    func wmc_register(cellClasses: [UICollectionViewCell.Type]) {
        cellClasses.forEach { cellClass in
            wmc_register(cellClass: cellClass)
        }
    }
    
    func wmc_dequeueReusableCell<T: UICollectionViewCell>(cellClass: T.Type, for indexPath: IndexPath) -> T {
        let identifier = Self.identifier(for: cellClass)
        let cell = dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) as! T
        return cell
    }
    
    func wmc_registerSupplementaryView(aClass: UICollectionReusableView.Type, kind: String) {
        let identifier = Self.identifier(for: aClass)
        register(aClass, forSupplementaryViewOfKind: kind, withReuseIdentifier: identifier)
    }
    
    func wmc_dequeueSupplementaryView<T: UICollectionReusableView>(aClass: T.Type, kind: String, for indexPath: IndexPath) -> T {
        let identifier = Self.identifier(for: aClass)
        let view = dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: identifier, for: indexPath) as! T
        return view
    }
    
    /// 根据Cell类型返回Cell重用标识符
    private static func identifier(for cellClass: AnyClass) -> String {
        let className = String(describing: cellClass)
        return className
    }
}
